package info.guoyang.basic.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletResponse;

/*
 * �����֤��
 */
public class ImageUtil {

	/** ����ַ� */
	private String randomCode = null;

	public ImageUtil(ServletResponse response) {
		this.response = response;
		try {
			createPic();
		} catch (Exception e) {
		}
	}

	private ServletResponse response = null;

	/**
	 * ��ȡ����ַ�
	 * 
	 * @return
	 */
	public String getRandomCode() {
		return randomCode;
	}

	private void createPic() throws IOException {
		// ���ڴ��д���ͼ��
		int width = 60;
		int height = 20;
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);

		// ��ȡͼ��������
		Graphics g = image.createGraphics();

		// �趨����ɫ
		g.setColor(getRandColor(200, 250));
		g.fillRect(0, 0, width, height);

		// �趨����
		g.setFont(new Font("Times New Roman", Font.PLAIN, 18));

		// ���߿�
		g.setColor(new Color(202, 237, 224));
		g.drawRect(1, 1, width - 1, height - 1);

		// ������155������ߣ�ʹͼ���е���֤�벻�ױ��������̽�⵽
		g.setColor(getRandColor(160, 200));
		for (int i = 0; i < 155; i++) {
			int x = (int) (Math.random() * ((double) (width)));
			int y = (int) (Math.random() * ((double) (height)));
			int xl = (int) (Math.random() * (12.0D));
			int yl = (int) (Math.random() * (12.0D));
			g.drawLine(x, y, x + xl, y + yl);
		}

		// ȡ���������֤��(4λ����)
		String sRand = "";
		for (int i = 0; i < 4; i++) {
			String rand = getRandStr();
			sRand += rand;
			// ����֤����ʾ��ͼ����
			// g.setColor(new Color(20 + (int)(Math.random()*(110.0D)), 20 +
			// (int)(Math.random()*(110.0D)), 20 +
			// (int)(Math.random()*(110.0D))));
			g.setColor(getRandColor(20, 130));
			g.drawString(rand, 13 * i + 6, 16);
		}

		// ͼ����Ч
		g.dispose();

		// ����ͼƬ
		// File myPic = new File(this.filePath + "myCheckPic" + sRand +
		// ".jpeg");
		// if (myPic.exists()) {
		// boolean hasNo = myPic.delete();
		// }

		// myPic.createNewFile();
		ImageIO.write(image, "JPEG", response.getOutputStream());

		// this.fileName = myPic.getName();
		this.randomCode = sRand;
	}

	/**
	 * ��Χ��������ɫ
	 * 
	 * @param fc
	 * @param bc
	 * @return
	 */
	private Color getRandColor(int fc, int bc) {
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int r = fc + (int) (Math.random() * ((double) (bc - fc)));
		int g = fc + (int) (Math.random() * ((double) (bc - fc)));
		int b = fc + (int) (Math.random() * ((double) (bc - fc)));
		return new Color(r, g, b);
	}

	/**
	 * �������ַ� a-z A-Z 0-9
	 * 
	 * @return
	 */
	private String getRandStr() {
		String[] strList = new String[62];

		for (int i = 0; i < 52; i++) {
			if (i < 26)
				strList[i] = Character.toString((char) (65 + i));
			else
				strList[i] = Character.toString((char) (71 + i));
		}

		for (int i = 0; i < 10; i++) {
			strList[52 + i] = String.valueOf(i);
		}
		double testD = Math.random() * 62.0D;
		int test = (int) testD;

		String randomStr = strList[test];

		return randomStr;
	}
}
